<template>
  <z-modal
    title="请选择委派人"
    width="600px"
    v-bind="modalOption"
    :okText="false"
    @ok="handleOk"
    @cancel="handleCancel"
  >
      <a-form-model layout="horizontal" labelAlign="right" ref="ruleForm" :label-col="labelCol" :wrapper-col="wrapperCol" :model="form" :rules="validatorRules">
        <a-form-model-item label="委派人" prop="delegate">
          <ZSelectUserByDep :button="false" :multi="false" v-model="form.delegate" />
        </a-form-model-item>
      </a-form-model>
  </z-modal>
</template>

<script>
import ModalMixins from '@/mixins/ModalMixins'
import ZSelectUserByDep from '@/components/zzbiz/ZSelectUserByDep'
import { postAction } from '@/services/manage'
import { mapGetters } from 'vuex'
export default {
  name: 'DelegateModal',
  components: {
    ZSelectUserByDep
  },
  mixins: [ModalMixins],
  data() {
    return {
      validatorRules: {
        delegate: [{ required: true, message: '请选择委派人' }]
      },
      url: {
        delegate: '/camunda/task/delegate/'
      },
      form: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 17 }
      },
      task: {}
    }
  },
  computed: {
    ...mapGetters('account', ['user'])
  },
  methods: {
    show(task) {
      this.task = task
      this.visible = true
    },
    handleOk() {
      this.confirmLoading = true
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (this.form.delegate === this.user.username) {
            this.$message.warning('不能委派自己')
            this.confirmLoading = false
            return
          }
          this.delegate()
        }
      })
    },
    delegate() {
      postAction(this.url.delegate + this.task.id + '/' + this.form.delegate).then(res => {
        if (res.success) {
          this.$message.success('委派成功')
          this.confirmLoading = false
          this.$emit('ok')
          this.close()
        }
      })
    }
  }
}
</script>

